package com.example.demo.controller;


import com.example.demo.bean.PageConfig;
import com.example.demo.service.PageConfigService;
import com.example.demo.service.XfyunTtsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TtsController {


    @Autowired
    private XfyunTtsService ttsService;




    @PostMapping(value = "/api/tts", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> textToSpeech(@RequestParam("id") Long id) {
        try {



            byte[] audioData = ttsService.textToSpeech(id);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.parseMediaType("audio/mpeg"));
            headers.setContentDispositionFormData("attachment", "speech.mp3");

            return ResponseEntity.ok()
                    .headers(headers)
                    .body(audioData);
        } catch (Exception e) {

            return ResponseEntity.internalServerError()
                    .body(("语音合成失败: " + e.getMessage()).getBytes());
        }
    }

    @PostMapping(value = "/api/tts/auto", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> autoTextToSpeech() {
//        // 固定的欢迎文本
//        String welcomeText = "5000年前，一座恢弘壯觀的城市在此興起。它代表了中華文明起源階段稻作農業的最高成就，並經由它所代表的良渚文化對其後五千年的中華文明發展擁有廣泛而深遠的影響，可實證中華文明的發展特徵——多元一體，並真實、完整地保存至今，它是人類文明發展史上具有傑出代表性的東亞地區史前大型聚落遺址——良渚古城遺址。";
//
//        try {
//            byte[] audioData = ttsService.textToSpeech(welcomeText);
//
//            HttpHeaders headers = new HttpHeaders();
//            headers.setContentType(MediaType.parseMediaType("audio/mpeg"));
//            headers.setContentDispositionFormData("attachment", "welcome.mp3");
//
//            return ResponseEntity.ok()
//                    .headers(headers)
//                    .body(audioData);
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println(e.getMessage());
//            return ResponseEntity.internalServerError()
//                    .body(("语音合成失败: " + e.getMessage()).getBytes());
//        }
        return  null;
    }
}
